import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;

import com.google.gson.Gson;
import com.mysql.jdbc.Connection;

import model.SystemConfiguration;
import nnsom.CallsSOM;
import nnsom.FeedRecord;
import nnsom.DataGenerator;

public class TestApp {
	static String connectionString = "jdbc:mysql://localhost/cdr_db?"
			+ "user=root&password=";
	static SystemConfiguration sc;

	public static void main(String[] args) {
		TestApp app = new TestApp();
		Gson gson = new Gson();
		BufferedReader br;
		try {
			br = new BufferedReader(new FileReader("parameters.json"));
			SystemConfiguration sc = gson.fromJson(br,
					SystemConfiguration.class);
			System.out.println(sc.toString());
			connectionString = String.format(
					"jdbc:mysql://%s:%d/%s?user=%s&password=%s", sc.getRdbms()
							.getIp(), sc.getRdbms().getPort(), sc.getDbName(),
					sc.getRdbms().getUser(), sc.getRdbms().getPassword());
			System.out.println(connectionString);
			app.launch();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void launch() {
		Calendar begin_time = Calendar.getInstance();
		// begin_time is 01/07/2010 00:00:00
		begin_time.clear();
		begin_time.set(Calendar.YEAR, 2010);
		begin_time.set(Calendar.MONTH, 6); // 6 is the 7th month
		begin_time.set(Calendar.DATE, 1);
		begin_time.set(Calendar.HOUR_OF_DAY, 0);
		begin_time.set(Calendar.MINUTE, 0);
		begin_time.set(Calendar.SECOND, 0);
		begin_time.set(Calendar.MILLISECOND, 0);

		Calendar end_time = Calendar.getInstance();
		// end_time is 31/07/2010 00:00:00
		end_time.clear();
		end_time.set(Calendar.YEAR, 2010);
		end_time.set(Calendar.MONTH, 6);
		end_time.set(Calendar.DATE, 31);
		end_time.set(Calendar.HOUR_OF_DAY, 0);
		end_time.set(Calendar.MINUTE, 0);
		end_time.set(Calendar.SECOND, 0);
		end_time.set(Calendar.MILLISECOND, 0);

		java.sql.Connection connect = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connect = DriverManager.getConnection(connectionString);
		} catch (Exception e) {
			System.err
					.println("Error while establishing connection with connection string "
							+ connectionString);
			e.printStackTrace();
			try {
				connect.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
		}

		// String subscriber_id = "18673:24020182";
		String subscriber_id = "03775:87382511";

		DataGenerator tdg = new DataGenerator(connect, subscriber_id,
				begin_time, end_time);
		List<FeedRecord> trainData = tdg.generateFeedRecordsStoredProcedure(60);
		for (FeedRecord fr : trainData) {
			System.out.println(fr.toString());
		}

		CallsSOM som = new CallsSOM(trainData);
		som.train();
		som.printOutputFrequencies();

		// String s[] = subscriber_id.split(":");
		// String fileName = String.format("som_%s_%s.eg", s[0], s[1]);
		// System.out.println("Saving SOM NN in " + fileName);
		// som.persistSOM(fileName);

		for (int i = 0; i < 24; i++) {
			System.out
					.println("-----------------------------------------------------------------");
			FeedRecord fr = tdg.liveData();
			if (fr == null) {
				System.out.println("No data");
			} else {
				som.processLiveData(fr);
			}
			tdg.oneHourForward();
		}

		System.out.println("###########FRAUD ATTEMPTS#############");
		som.processFraudData1();
		som.processFraudData2();
	}
}
